import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:openinstall_flutter_plugin/openinstall_flutter_plugin.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/states/states_model/user.dart';

class OpenInstall extends StatefulWidget {
  final Widget child;

  OpenInstall({this.child, Key key}) : super(key: key);

  @override
  OpenInstallState createState() => OpenInstallState();
}

class OpenInstallState extends State<OpenInstall> {
  OpeninstallFlutterPlugin _openinstallFlutterPlugin;

  String code;

  @override
  void initState() {
    super.initState();
    _openinstallFlutterPlugin = OpeninstallFlutterPlugin();
    _openinstallFlutterPlugin.init((Map<String, dynamic> data) async {});
    _openinstallFlutterPlugin.install((Map<String, dynamic> data) async {
      final bindData = jsonDecode(data['bindData']);
      if (!empty(bindData) && bindData is Map) {
        code = bindData['share'];
      }
    });
  }

  // @override
  // void didChangeDependencies() {
  //   super.didChangeDependencies();
  //   // 监听用户信息改变
  //   // 用户登录后，并且未绑定邀请信息，同时app存在邀请码
  //   // 进行app绑定
  //   Provide.value<UserState>(context).addListener(() {
  //     if (code == null) return;
  //     final token = Provide.value<UserState>(context)?.info?.memberToken;
  //     if (token == null) return;
  //     UserApis.isinvitationcode(token).then((bool isset) {
  //       if (!isset) {
  //         UserApis.sharehref(token, code);
  //         Provide.value<UserState>(context).userInfo();
  //       }
  //     });
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget.child,
    );
  }
}
